package com.cskaoyan.demo4.listener;

import com.cskaoyan.demo4.config.ApplicationConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * @Description：
 * @Author：BLUEBLANK
 * @Date：2024/4/18 21:49
 */

@WebListener
public class ApplicationInitializationServletContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        // 创建Spring容器，在容器中注册组件
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
        // 把容器放在ServletContext中在整个应用中共享 → Servlet里要使用
        sce.getServletContext().setAttribute("ApplicationContext", applicationContext);
    }
}
